/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import ejb.negocio.UsuarioServiceLocal;
import javax.ejb.EJB;
//import javax.faces.bean.ManagedBean;
//import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import entidades.Usuario;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
//import javax.annotation.ManagedBean;

/**
 *
 * @author Pablo
 */

//@EJB (name="usuarioEJB", beanInterface=UsuarioServiceLocal.class)

//@ManagedBean(name = "usuarioSesion")
@javax.inject.Named("usuarioSesion")
@SessionScoped
public class UsuarioController extends ParentController implements Serializable{
   
    private String nick;
    private String password;
    private boolean login;
    private Usuario usuarioLogged;
    private UIComponent mybutton;
           
    FacesContext context;
    HttpSession session;
            
    @EJB
    private UsuarioServiceLocal usuario ;
    
    /**
     * Creates a new instance of UsuarioController
     */
    public UsuarioController() {
        login = false;
    }
    
    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isLogin() {
        return login;
    }

    public void setLogin(boolean login) {
        this.login = login;
    }
    
    public UsuarioServiceLocal getUsuario() {
        return usuario;
    }

    public void setUsuario(UsuarioServiceLocal usuario) {
        this.usuario = usuario;
    }
    
    public UIComponent getMybutton() {
        return mybutton;
    }

    public void setMybutton(UIComponent mybutton) {
        this.mybutton = mybutton;
    }
    
    public String doLogin (){
        /*
        Context ctx;
        try {
            ctx = new InitialContext();
            usuario = (UsuarioServiceLocal) ctx.lookup("java:comp/env/usuarioEJB");
        } catch (NamingException ex) {
            Logger.getLogger(UsuarioController.class.getName()).log(Level.SEVERE, null, ex);
        }        
        */
        context = FacesContext.getCurrentInstance();
        session = (HttpSession) context.getExternalContext().getSession(true);
        
        usuarioLogged = usuario.abrirSesion(nick, password);
        login = true;
        if (usuarioLogged == null) login = false;
        if (login == false){            
            createMessageError(mybutton.getClientId(context),"incorrectdata");  
            return "index";
        }
        else{
            session.setAttribute("usuario", usuarioLogged);
            return "menupage";
        }
    }
    
    public String goRegistro(){
        return "registro";
    }
}
